package pers.qianyu.month_202102.date_20210223;

import org.junit.*;

/**
 * 1052. 爱生气的书店老板
 * https://leetcode-cn.com/problems/grumpy-bookstore-owner/
 *
 * @author mizzle rain
 * @date 2021-02-23 9:49
 */
public class MaxSatisfied {
    public int maxSatisfied(int[] customers, int[] grumpy, int X) {
        int n = customers.length;
        int res = 0;
        for (int i = 0; i < n; i++) if (grumpy[i] == 0) res += customers[i];

        int t = 0, max = 0;
        for (int left = 0, right = 0; right < n; right++) {
            if (grumpy[right] == 1) t += customers[right];
            if (right >= X) {
                if (grumpy[right - X] == 1) {
                    t -= customers[left];
                }
                left++;
            }
            max = Math.max(max, t);
        }
        return res + max;
    }

    @Test
    public void test1() {
        int result = new MaxSatisfied().maxSatisfied(
                new int[]{1, 0, 1, 2, 1, 1, 7, 5},
                new int[]{0, 1, 0, 1, 0, 1, 0, 1}, 3);
        Assert.assertEquals(16, result);
    }
}
